<html>
<head><meta charset="utf-8"><title>PartialEq on for &lt;&#x27;a&gt; fn(&amp;&#x27;a _) #46989 · t-compiler · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/index.html">t-compiler</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/PartialEq.20on.20for.20.3C&#x27;a.3E.20fn(.26&#x27;a.20_).20.2346989.html">PartialEq on for &lt;&#x27;a&gt; fn(&amp;&#x27;a _) #46989</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="177506336"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/PartialEq%20on%20for%20%3C%27a%3E%20fn%28%26%27a%20_%29%20%2346989/near/177506336" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> pnkfelix <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/PartialEq.20on.20for.20.3C&#x27;a.3E.20fn(.26&#x27;a.20_).20.2346989.html#177506336">(Oct 07 2019 at 10:16)</a>:</h4>
<p>I'd like to idly discuss this detail of our current std derives (or lack thereof) for <code>for &lt;'a&gt; T</code> ...</p>



<a name="177506379"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/PartialEq%20on%20for%20%3C%27a%3E%20fn%28%26%27a%20_%29%20%2346989/near/177506379" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> pnkfelix <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/PartialEq.20on.20for.20.3C&#x27;a.3E.20fn(.26&#x27;a.20_).20.2346989.html#177506379">(Oct 07 2019 at 10:17)</a>:</h4>
<p>a reasonably large set of people think behavior is unintuitive on <a href="https://play.rust-lang.org/?version=nightly&amp;mode=debug&amp;edition=2018&amp;gist=34bb2c64aa548bde034e655aee759522" target="_blank" title="https://play.rust-lang.org/?version=nightly&amp;mode=debug&amp;edition=2018&amp;gist=34bb2c64aa548bde034e655aee759522">this playpen</a></p>



<a name="177506460"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/PartialEq%20on%20for%20%3C%27a%3E%20fn%28%26%27a%20_%29%20%2346989/near/177506460" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> pnkfelix <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/PartialEq.20on.20for.20.3C&#x27;a.3E.20fn(.26&#x27;a.20_).20.2346989.html#177506460">(Oct 07 2019 at 10:19)</a>:</h4>
<p>hmm after thinking about it, I guess its hard/impossible to "just fix this" via hard-coding our own impls</p>



<a name="177506599"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/PartialEq%20on%20for%20%3C%27a%3E%20fn%28%26%27a%20_%29%20%2346989/near/177506599" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> pnkfelix <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/PartialEq.20on.20for.20.3C&#x27;a.3E.20fn(.26&#x27;a.20_).20.2346989.html#177506599">(Oct 07 2019 at 10:21)</a>:</h4>
<p>/me goes skimming code to figure out how we even <code>impl PartialEq for fn</code> in general</p>



<a name="177817762"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/PartialEq%20on%20for%20%3C%27a%3E%20fn%28%26%27a%20_%29%20%2346989/near/177817762" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/PartialEq.20on.20for.20.3C&#x27;a.3E.20fn(.26&#x27;a.20_).20.2346989.html#177817762">(Oct 10 2019 at 14:54)</a>:</h4>
<p>As we discussed together, <span class="user-mention" data-user-id="116083">@pnkfelix</span>, something I've considered from time to time (and am still vaguely in favor of, presuming there is no complication) is creating a built-in "marker trait" called <code>FnPointer</code>, which is implemented for any <code>fn(...)</code> type (of any ABI etc). This trait might look something like:</p>
<div class="codehilite"><pre><span></span><span class="cp">#[fundamental]</span><span class="w"></span>
<span class="k">trait</span><span class="w"> </span><span class="n">FnPointer</span>: <span class="nb">Copy</span> <span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="k">fn</span> <span class="nf">as_pointer</span><span class="p">(</span><span class="bp">self</span><span class="p">)</span><span class="w"> </span>-&gt; <span class="o">*</span><span class="k">const</span><span class="w"> </span><span class="p">();</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
</pre></div>


<p>It would be built-in to the compiler and illegal to manually implement. The trait system would just hard-code it in a simple way.</p>
<p>I think this would allow us to do things like</p>
<div class="codehilite"><pre><span></span><span class="k">impl</span><span class="o">&lt;</span><span class="n">T</span>: <span class="nc">FnPointer</span><span class="o">&gt;</span><span class="w"> </span><span class="nb">Clone</span><span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="n">T</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="p">..</span><span class="w"> </span><span class="p">}</span><span class="w"></span>
<span class="k">impl</span><span class="o">&lt;</span><span class="n">T</span>: <span class="nc">FnPointer</span><span class="o">&gt;</span><span class="w"> </span><span class="nb">PartialEq</span><span class="w">  </span><span class="k">for</span><span class="w"> </span><span class="n">T</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="p">..</span><span class="w"> </span><span class="p">}</span><span class="w"></span>
<span class="k">impl</span><span class="o">&lt;</span><span class="n">T</span>: <span class="nc">FnPointer</span><span class="o">&gt;</span><span class="w"> </span><span class="n">Debug</span><span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="n">T</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="p">..</span><span class="w"> </span><span class="p">}</span><span class="w"></span>
</pre></div>


<p>and so forth. There <em>may</em> be some complications around fundamental that I'm not thinking of, though. Could be "prototyped" fairly easily I guess with just a regular trait that is impemented for <code>fn(A)</code>, <code>fn(A, B)</code>, etc.</p>



<a name="178275238"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/PartialEq%20on%20for%20%3C%27a%3E%20fn%28%26%27a%20_%29%20%2346989/near/178275238" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> pnkfelix <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/PartialEq.20on.20for.20.3C&#x27;a.3E.20fn(.26&#x27;a.20_).20.2346989.html#178275238">(Oct 16 2019 at 10:11)</a>:</h4>
<blockquote>
<p>/me goes skimming code to figure out how we even <code>impl PartialEq for fn</code> in general</p>
</blockquote>
<p>For those wondering where the code was, it is in <code>src/libcore/ptr/mod.rs</code>: <a href="https://github.com/rust-lang/rust/blob/master/src/libcore/ptr/mod.rs#L2801" target="_blank" title="https://github.com/rust-lang/rust/blob/master/src/libcore/ptr/mod.rs#L2801">https://github.com/rust-lang/rust/blob/master/src/libcore/ptr/mod.rs#L2801</a></p>



<a name="178275365"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/PartialEq%20on%20for%20%3C%27a%3E%20fn%28%26%27a%20_%29%20%2346989/near/178275365" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> pnkfelix <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/PartialEq.20on.20for.20.3C&#x27;a.3E.20fn(.26&#x27;a.20_).20.2346989.html#178275365">(Oct 16 2019 at 10:13)</a>:</h4>
<p><span class="user-mention" data-user-id="116009">@nikomatsakis</span> would your hypothetical <code>trait FnPointer</code>  also get magically implemented by the type <code>for &lt;'a&gt; fn(&amp;'a u32)</code>, and <code>for &lt;'a&gt; fn(ParameterizedOver&lt;'a&gt;) -&gt; AlsoOver&lt;'a&gt;</code>, etc ?</p>



<a name="178275421"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/PartialEq%20on%20for%20%3C%27a%3E%20fn%28%26%27a%20_%29%20%2346989/near/178275421" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> pnkfelix <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/PartialEq.20on.20for.20.3C&#x27;a.3E.20fn(.26&#x27;a.20_).20.2346989.html#178275421">(Oct 16 2019 at 10:14)</a>:</h4>
<p>(that bit of magic does not seem as "easy" to prototype)</p>



<a name="178275531"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/PartialEq%20on%20for%20%3C%27a%3E%20fn%28%26%27a%20_%29%20%2346989/near/178275531" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> pnkfelix <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/PartialEq.20on.20for.20.3C&#x27;a.3E.20fn(.26&#x27;a.20_).20.2346989.html#178275531">(Oct 16 2019 at 10:16)</a>:</h4>
<p>(but it does seem plausible to add support for this to the compiler. I.e. rustc presumably could dive into <code>for &lt;'a&gt; ...</code> (recursively over repeated <code>for &lt;'b&gt;</code> etc) until it find a fn-ptr and can then say "Okay, done, its an <code>FnPointer</code>!)</p>



<a name="178752690"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/PartialEq%20on%20for%20%3C%27a%3E%20fn%28%26%27a%20_%29%20%2346989/near/178752690" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/PartialEq.20on.20for.20.3C&#x27;a.3E.20fn(.26&#x27;a.20_).20.2346989.html#178752690">(Oct 22 2019 at 12:59)</a>:</h4>
<blockquote>
<p><span class="user-mention silent" data-user-id="116009">nikomatsakis</span> would your hypothetical <code>trait FnPointer</code>  also get magically implemented by the type <code>for &lt;'a&gt; fn(&amp;'a u32)</code>, and <code>for &lt;'a&gt; fn(ParameterizedOver&lt;'a&gt;) -&gt; AlsoOver&lt;'a&gt;</code>, etc ?</p>
</blockquote>
<p>I'm not sure what's hard about that, <span class="user-mention" data-user-id="116083">@pnkfelix</span></p>



<a name="178752712"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/PartialEq%20on%20for%20%3C%27a%3E%20fn%28%26%27a%20_%29%20%2346989/near/178752712" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/PartialEq.20on.20for.20.3C&#x27;a.3E.20fn(.26&#x27;a.20_).20.2346989.html#178752712">(Oct 22 2019 at 12:59)</a>:</h4>
<p>oh, for <em>prototyping</em>, you are saying</p>



<a name="178752751"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/PartialEq%20on%20for%20%3C%27a%3E%20fn%28%26%27a%20_%29%20%2346989/near/178752751" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/PartialEq.20on.20for.20.3C&#x27;a.3E.20fn(.26&#x27;a.20_).20.2346989.html#178752751">(Oct 22 2019 at 13:00)</a>:</h4>
<p>I mean the "prototype" would presuamably be implemented for <code>fn(A)</code>, <code>fn(A, B)</code> etc</p>



<a name="178752793"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/PartialEq%20on%20for%20%3C%27a%3E%20fn%28%26%27a%20_%29%20%2346989/near/178752793" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/PartialEq.20on.20for.20.3C&#x27;a.3E.20fn(.26&#x27;a.20_).20.2346989.html#178752793">(Oct 22 2019 at 13:00)</a>:</h4>
<p>just as we do today for many traits</p>



<a name="178752802"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/PartialEq%20on%20for%20%3C%27a%3E%20fn%28%26%27a%20_%29%20%2346989/near/178752802" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/PartialEq.20on.20for.20.3C&#x27;a.3E.20fn(.26&#x27;a.20_).20.2346989.html#178752802">(Oct 22 2019 at 13:00)</a>:</h4>
<p>which means it wouldn't handle higher-ranked things</p>



<a name="178752819"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/PartialEq%20on%20for%20%3C%27a%3E%20fn%28%26%27a%20_%29%20%2346989/near/178752819" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> pnkfelix <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/PartialEq.20on.20for.20.3C&#x27;a.3E.20fn(.26&#x27;a.20_).20.2346989.html#178752819">(Oct 22 2019 at 13:00)</a>:</h4>
<p>which is the primary thing I think I want addressed here</p>



<a name="178752821"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/PartialEq%20on%20for%20%3C%27a%3E%20fn%28%26%27a%20_%29%20%2346989/near/178752821" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/PartialEq.20on.20for.20.3C&#x27;a.3E.20fn(.26&#x27;a.20_).20.2346989.html#178752821">(Oct 22 2019 at 13:00)</a>:</h4>
<p>I guess we probably do <code>impl&lt;A, R&gt; Trait for fn(A) -&gt; R</code> or something now?</p>



<a name="178752834"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/PartialEq%20on%20for%20%3C%27a%3E%20fn%28%26%27a%20_%29%20%2346989/near/178752834" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/PartialEq.20on.20for.20.3C&#x27;a.3E.20fn(.26&#x27;a.20_).20.2346989.html#178752834">(Oct 22 2019 at 13:00)</a>:</h4>
<p>yeah so the point of prototyping would just be to check if the fundamental trait setup gives problems</p>



<a name="178752843"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/PartialEq%20on%20for%20%3C%27a%3E%20fn%28%26%27a%20_%29%20%2346989/near/178752843" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/PartialEq.20on.20for.20.3C&#x27;a.3E.20fn(.26&#x27;a.20_).20.2346989.html#178752843">(Oct 22 2019 at 13:00)</a>:</h4>
<p>the higher-ranked stuff would come from making it a lang item</p>



<a name="178752851"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/PartialEq%20on%20for%20%3C%27a%3E%20fn%28%26%27a%20_%29%20%2346989/near/178752851" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> pnkfelix <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/PartialEq.20on.20for.20.3C&#x27;a.3E.20fn(.26&#x27;a.20_).20.2346989.html#178752851">(Oct 22 2019 at 13:00)</a>:</h4>
<p>I guess that makes sense</p>



<a name="178752879"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/PartialEq%20on%20for%20%3C%27a%3E%20fn%28%26%27a%20_%29%20%2346989/near/178752879" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> pnkfelix <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/PartialEq.20on.20for.20.3C&#x27;a.3E.20fn(.26&#x27;a.20_).20.2346989.html#178752879">(Oct 22 2019 at 13:01)</a>:</h4>
<p>make sure the overall design doesnt have fundamntal problems before adding the new feature</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>